﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;

using Wxits.Data;
using Wxits.Helper;


public partial class Admin_GetCash : BasePage_Grid
{
    private string _id = string.Empty;


    private int _PageSize
	{
		set { ViewState["PageSize"] = value; }
		get
		{
			return Convert.ToInt32(ViewState["PageSize"]);
		}
	}

	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			_PageSize = 10;


            DataCheck.CheckAuthority();


            ShowData(0, _PageSize);			
		}
	}

	private void ShowData(long PageIndex, long PageSize)
	{


        string strSQL = @"
					SELECT a.* , b.NickName				
						,'StatusTxt' = 	
						CASE 
							WHEN a.status = 1 THEN N'待确认' 
							WHEN a.status = 2 THEN N'已支付' 
							ELSE N'已取消'
						END
					FROM Mall_Users_Bonus_Cash_Get a left join Users b on a.UserId = b.UserId
					WHERE 1 = 1 ";

        if (!string.IsNullOrEmpty(txtName.Text.Trim()))
            strSQL += " AND (NickName like '%" + txtName.Text.Trim() + "%' or AccountName like '%" + txtName.Text.Trim() + "%')";
        if (!string.IsNullOrEmpty(txtStatus.SelectedValue))
            strSQL += " AND a.Status = '" + txtStatus.SelectedValue + "'";

        strSQL += " ORDER BY a.Id DESC";

        DataTable dt = DBAccess.ExecuteDataTable(strSQL);

        BindGrid(gvData, Navigator, PageIndex, PageSize, dt);
	}

	protected void Navigator_PageChanged(object sender, QHH.Web.UI.EventArg.NavigatorEventArgs e)
	{
		_PageSize = (int)e.PageSize;
		ShowData(e.NewPageIndex, _PageSize);
	}

	protected void Search_Click(object sender, EventArgs e)
	{
		ShowData(0, _PageSize);
	}

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string strSQL = string.Empty;
        for (int i = 0; i < gvData.Rows.Count; i++)
        {
            HtmlInputCheckBox cb = (HtmlInputCheckBox)(gvData.Rows[i].FindControl("Chk_Cell"));
            if (!cb.Checked) continue;

            string ID = cb.Value;

            //删除组 用户关系表
            //strSQL += string.Format("DELETE FROM GL_G05_UserRight WHERE UserID = '{0}'; ", ID);
            strSQL = @"SELECT * FROM Mall_Users_Bonus_Cash_Get WHERE id = '{0}'";
            DataTable dt = DBAccess.ExecuteDataTable(string.Format(strSQL, ID));
            strSQL = string.Empty;
            if (dt.Rows[0]["status"].ToString() == "1")
            {
                strSQL = string.Format( @"	IF((SELECT COUNT(*) FROM Mall_Statistics_Day WHERE CONVERT(varchar(10),cdate,21) = CONVERT(varchar(10),GETDATE(),21) ) = 0)
		                    INSERT Mall_Statistics_Day (cdate) VALUES (GETDATE())

                        UPDATE Mall_Statistics_Day SET TotalGetCash = TotalGetCash + CONVERT(MONEY,'{0}')  WHERE CONVERT(varchar(10),cdate,21) = CONVERT(varchar(10),GETDATE(),21) 
		                UPDATE Mall_Statistics_Total SET TotalGetCash = TotalGetCash + CONVERT(MONEY,'{0}')", dt.Rows[0]["Amount"].ToString());

                strSQL += string.Format("INSERT Mall_Logs (UserName,UserType,LogsType,Description,IP,UserAgent) VALUES ('{0}',2 , 4 ,'{1}','{2}','{3}'); ", HelpCookie.Get("AdminUserName"), "批量审核确认支付 支付记录Id:" + ID, Common.GetClientIp(), Request.ServerVariables["HTTP_USER_AGENT"]);
                strSQL += string.Format("UPDATE Mall_Users_Bonus_Cash_Get SET status = 2, Remark=Remark+'|批量审核确认支付', AdminUserName=AdminUserName+'|{1}' WHERE id = '{0}'", ID, HelpCookie.Get("AdminUserName"));

            }
            //if (dt.Rows[0]["status"].ToString() == "2")
            //{
            //    strSQL = string.Format(@"	IF((SELECT COUNT(*) FROM Mall_Statistics_Day WHERE CONVERT(varchar(10),cdate,21) = CONVERT(varchar(10),GETDATE(),21) ) = 0)
		          //          INSERT Mall_Statistics_Day (cdate) VALUES (GETDATE())

            //            UPDATE Mall_Statistics_Day SET TotalGetCash = TotalGetCash - CONVERT(MONEY,'{0}')  WHERE CONVERT(varchar(10),cdate,21) = CONVERT(varchar(10),GETDATE(),21) 
		          //      UPDATE Mall_Statistics_Total SET TotalGetCash = TotalGetCash - CONVERT(MONEY,'{0}')", dt.Rows[0]["Amount"].ToString());

            //    strSQL += string.Format("INSERT Logs (UserName,UserType,LogsType,Description,IP,UserAgent) VALUES ('{0}',2 , 4 ,'{1}','{2}','{3}'); ", HelpCookie.Get("AdminUserName"), "批量取消支付审核 支付记录Id:" + ID, Common.GetClientIp(), Request.ServerVariables["HTTP_USER_AGENT"]);
            //    strSQL += string.Format("UPDATE Mall_Users_Bonus_Cash_Get SET status = 1, Remark=Remark+'|批量取消支付审核', AdminUserName=AdminUserName+'|{1}' WHERE id = '{0}'", ID, HelpCookie.Get("AdminUserName"));

            //}
            if (!string.IsNullOrEmpty(strSQL))
            {
                DBAccess.ExecuteNonQuery(strSQL);
            }
        }

        ShowData(0, _PageSize);
    }
}
